package com.bw.mallshop.mvvm.viewmodel;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.adapter.HomeGoodsAdapter;
import com.bw.mallshop.annotation.CheckLogin;
import com.bw.mallshop.dao.UserSingle;
import com.bw.mallshop.entity.AddCarEntity;
import com.bw.mallshop.entity.CarListEntity;
import com.bw.mallshop.entity.HomeDedtailEntity;
import com.bw.mallshop.entity.HomeGoodsEntity;
import com.bw.mallshop.entity.HomeLoadEntity;
import com.bw.mallshop.mvvm.model.GoodsValuesModel;
import com.bw.mallshop.mvvm.view.CarListActivity;
import com.bw.mallshop.mvvm.view.FrameActivity;
import com.bw.mallshop.mvvm.view.HomeDetailActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class GoodsValuesViewModel extends BaseViewModel<GoodsValuesModel> implements
        OnRefreshLoadMoreListener , OnItemClickListener {

    private int page = 1;//当前列表中的page页数
    private int categoryId = 14;//分类id
    private int goodsId = 0;
    public ObservableField<HomeDedtailEntity> entity=new ObservableField<>();
    public HomeGoodsAdapter adapter;

    public void setGoodsId(int goodsId) {
        this.goodsId = goodsId;
    }

    @Override
    protected void initData() {
        adapter = new HomeGoodsAdapter();
        adapter.setOnItemClickListener(this);
        requestDetails();
    }

    //获取商品详情
    public void requestDetails(){
        //商品详情map
        Map<String,Object>map = new HashMap<>();
        map.put("goodsId",goodsId);
        //推荐商品map
        Map<String,Object>recommandGoods = recommandGoods();
        m.requestAll(map,recommandGoods).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //获取推荐商品
    private Map<String,Object>recommandGoods(){
        page = 1;
        Map<String,Object>map=new HashMap<>();
        map.put("categoryId",categoryId);
        map.put("pageNo",page);
        return map;
    }

    //分页加载推荐商品
    private void loadGoods(){
        page += 1;
        Map<String,Object>map=new HashMap<>();
        map.put("categoryId",categoryId);
        map.put("pageNo",page);
        m.requestLoad(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        //区分服务器返回值
        if (entity instanceof CarListEntity){
            showErro("添加离线购物车成功");
            finishPage();
        }else if (entity instanceof HomeDedtailEntity){
            //商品详情数据
            HomeDedtailEntity homeDedtailEntity = (HomeDedtailEntity) entity;
            this.entity.set(homeDedtailEntity);
        }else if (entity instanceof HomeGoodsEntity){
            HomeGoodsEntity goodsEntity = (HomeGoodsEntity) entity;
            if (goodsEntity.getData()==null)
                return;
            //下拉刷新获取推荐商品数据
            adapter.setNewInstance(goodsEntity.getData());
        }else if (entity instanceof HomeLoadEntity){
            //上拉加载推荐商品
            HomeLoadEntity loadEntity = (HomeLoadEntity) entity;
            if (loadEntity.getData() == null)
                return;
            adapter.addData(loadEntity.getData());
        }else if (entity instanceof AddCarEntity){
            AddCarEntity addCarEntity = (AddCarEntity) entity;
            if (addCarEntity.message.equals("添加购物车成功")){
                showErro("添加购物车成功");
                finishPage();
            }
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        loadGoods();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();//关闭刷新头
        requestDetails();
    }

    //返回页面
    public void finish(){
        finishPage();
    }
    //返回二级菜单
    public void backMenu(){
        startPage(FrameActivity.class);
    }
    //分享
    public void share(){

    }
    //加入购物车
//    @CheckLogin
    @SuppressLint("MissingPermission")
    public void addCar(){
        LogUtils.e("addCar()");
        //区分加入到那个购物车(离线在线)
        if (UserSingle.getInstance().isLogin()) {
            Map<String, Object> map = new HashMap<>();
            map.put("goodsCount","1");
            map.put("goodsDesc",entity.get().data.getGoods_desc());
            map.put("goodsIcon",entity.get().data.getGoods_default_icon());
            map.put("goodsId",entity.get().data.getId());
            map.put("goodsPrice",entity.get().data.getGoods_default_price());
            map.put("goodsSku",entity.get().data.getGoods_default_sku());
            map.put("time",System.currentTimeMillis()/1000);
            map.put("userId",UserSingle.getInstance().userEntityValues.id);
            m.requestAddCar(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            //添加离线购物车方法
            CarListEntity.Values values = new CarListEntity.Values();
            values.imie = PhoneUtils.getIMEI();
            values.goods_sku = entity.get().data.getGoods_default_sku();
            values.goods_price = entity.get().data.getGoods_default_price();
            values.goods_id = entity.get().data.getId();
            values.goods_icon = entity.get().data.getGoods_default_icon();
            values.goods_desc = entity.get().data.getGoods_desc();
            values.goods_count = 1;
            m.insertyCarDao(values).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }
    //购物车列表
    public void carList(){
        startPage(CarListActivity.class);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        HomeGoodsEntity.DataBean bean = this.adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putInt("goodsId",bean.getId());
        startPage(HomeDetailActivity.class,bundle);
    }
}
